using System;
using System.Reflection;
using CommandSystem;

namespace RgCore.Modules.Commands.SubPluginManagerCommands
{
    public class ListCommand<T> : ICommand where T : Attribute, IName
    {
        public ListCommand(string commandHead, SubPluginManager<T> manager)
        {
            CommandHead = commandHead;
            Manager = manager;
        }

        public string CommandHead { get; }
        public SubPluginManager<T> Manager { get; private set; }

        public string GetUsage() => $"用法: {Command} list";

        public bool Execute(ArraySegment<string> arguments, ICommandSender sender, out string response)
        {
            if (!sender.CheckPermission(PlayerPermissions.ServerConsoleCommands))
            {
                response = "你没有权限执行此命令";
                return false;
            }

            response = "已加载的项目：";
            foreach (var a in Manager.AllTypes)
                response += $"\n{a.Key.GetCustomAttribute<T>().Name} ({a.Key.Name})";
            response += "\n---------------------";
            return true;
        }

        public string Command { get; } = "list";
        public string[] Aliases { get; } = Array.Empty<string>();
        public string Description { get; } = "获取已加载的子项目列表";
    }
}